import { defineStore } from 'pinia'
import { systemSetting } from '@/settings/systemSetting'
import { asideCollapsedWidth } from '@/settings/designSetting'
import { ToolsStatusEnum, SettingStoreType, SettingStoreEnums } from './settingStore.d'
import { setLocalStorage, getLocalStorage } from '@/utils/storage'
import { StorageEnum } from '@/enums/storeageEnum'

const { GO_SETTING_STORE } = StorageEnum

const storageSetting: SettingStoreType = getLocalStorage(GO_SETTING_STORE)

export const useSettingStore = defineStore({
  id: 'useSettingStore',
  state: (): SettingStoreType => storageSetting || systemSetting,
  getters: {
    getHidePackageOneCategory(): boolean {
      return this[SettingStoreEnums.HIDE_PACKAGE_ONE_CATEGORY]
    },
    getChangeLangReload(): boolean {
      return this[SettingStoreEnums.CHANGE_LANG_RELOAD]
    },
    getAsideAllCollapsed(): boolean {
      return this[SettingStoreEnums.ASIDE_ALL_COLLAPSED]
    },
    getAsideCollapsedWidth(): number {
      return this[SettingStoreEnums.ASIDE_ALL_COLLAPSED] ? 0 : asideCollapsedWidth
    },
    getChartMoveDistance(): number {
      return this[SettingStoreEnums.CHART_MOVE_DISTANCE]
    },
    getChartAlignRange(): number {
      return this[SettingStoreEnums.CHART_ALIGN_RANGE]
    },
    getChartToolsStatus(): ToolsStatusEnum {
      return this[SettingStoreEnums.CHART_TOOLS_STATUS]
    },
    getChartToolsStatusHide(): boolean {
      return this[SettingStoreEnums.CHART_TOOLS_STATUS_HIDE]
    }
  },
  actions: {
    setItem<T extends keyof SettingStoreType, K extends SettingStoreType[T]>(key: T, value: K) {
      this.$patch(state => {
        state[key] = value
      })
      setLocalStorage(GO_SETTING_STORE, this.$state)
    }
  }
})
